<template>
  <div class="app-container calendar-list-container">
   <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入真实名称或是移动号码" v-model="search.realname"></el-input>
       <el-select @change='handleFilter' style="width: 160px" class="filter-item" v-model="search.role_id" placeholder="请选择用户角色">
          <el-option label="全部" value=''></el-option>
          <el-option v-for="option in roleOptions" :key="option.id"  :label="option.name"  :value="option.id"></el-option>
      </el-select>
      <el-button class="filter-item" type="primary" plain v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" v-waves type="primary" v-roles="'editAdmin'"  icon="el-icon-edit">添加管理员</el-button>
    </div>
    <!-- // 这是列表 -->
    <el-table :data="listData" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index"  align="center" label="序号" width="80"></el-table-column>
      <el-table-column align="center" label="真实名称">
        <template slot-scope="scope">
          <span>{{scope.row.realname}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="移动号码">
        <template slot-scope="scope">
          <span>{{scope.row.mobile}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色">
        <template slot-scope="scope">
          <span>{{scope.row.role_name}}</span>
        </template>
       </el-table-column>
      <el-table-column align="center" label="归属营业点" width="150">
         <template slot-scope="scope" > 
           <div v-for="item in scope.row.outlet_info" :key="item.id">
              <el-tag type="draft" style="margin:2px">{{item.hotel_name}}</el-tag>
           </div>
        </template>
      </el-table-column>
        <el-table-column align="center" label="上次登陆IP">
        <template slot-scope="scope">
          <span>{{scope.row.last_ip}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后登陆时间">
        <template slot-scope="scope">
          <span>{{scope.row.last_login}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户状态">
        <template slot-scope="scope">
          <el-button v-if="scope.row.id !=1" size="mini" :type="scope.row.status | statusFilter" plain @click="handleUserStatus(scope.row)">{{scope.row.status | statusName}}</el-button>
        </template>
      </el-table-column>
      <!-- //这里是操作   -->
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button v-if="scope.row.id !=1" size="mini" type="primary" @click="handlEdit(scope.row)" v-roles="'editAdmin'">编辑</el-button>
          <el-button v-if="scope.row.id !=1" size="mini" type="danger" @click="handlDel(scope.row)" v-roles="'deleteAdmin'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--用户编辑窗口  -->
    <edit-admin-user :visible.sync="editVisible" :propsData="propsData" @refresh='isrefresh' ref="triggerWay"></edit-admin-user>  
    <!--编辑窗口  END -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <!--可自定义按钮的样式、show/hide临界点、返回的位置  -->
    <!--如需文字提示，可在外部添加element的<el-tooltip></el-tooltip>元素  -->
    <el-tooltip placement="top" content="回到顶部">
      <back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
    </el-tooltip>
  </div>
</template>

<script>

import editAdminUser from '../permission/editAdminUser'
import { mapGetters } from 'vuex'
import BackToTop from '@/components/BackToTop' // 回到顶部
import * as api from '@/api/main-api' // API
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'adminUser',

  directives: {
    waves
  },

  components: { BackToTop, editAdminUser },
  data() {
    return {
      // 回到顶部样式
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'z-index': '9999',
        'border-radius': '4px',
        'line-height': '45px', // 请保持与高度一致以垂直居中 Please keep consistent with height to center vertically
        background: '#e7eaf1'// 按钮的背景颜色 The background color of the button
      },

      // 长带参数
      listQuery: {
        page: 1,
        pagesize: 10
      },

      // 搜索字段
      search: {
        realname: '',
        role_id: ''
      },
      total: 0,
      listLoading: false,
      listData: [],
      roleOptions: [],

      // 编辑传值
      editVisible: false,
      propsData: {}
    }
  },

  computed: {
    ...mapGetters([
      'outlet_id'
    ])
  },

  // 初始化数据
  created() {
    // 传过来的数据
    this.listQuery.unit_id = this.outlet_id
    if (this.$route.query.role_id) {
      this.search.role_id = this.$route.query.role_id
      this.listQuery = Object.assign(this.listQuery, this.search)
    }
    this.fetchData()
    this.roleList()
  },

  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    },
    statusName(status) {
      return (status === '1') ? '正常' : '无效用户'
    }
  },
  // 触动方法
  methods: {
    // 控制每页调数
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.fetchData()
    },

    // 跳页
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },

    // 搜搜
    handleFilter() {
      this.listQuery.page = 1
      // 复制一个对象<br>var obj = { a: 1 ,b:2};
      if (this.search_time) {
        this.search.stime = this.search_time[0]
        this.search.etime = this.search_time[1]
      }
      this.listQuery = Object.assign(this.listQuery, this.search)
      this.fetchData()
    },

    // 添加
    handleCreate() {
      this.editVisible = !this.editVisible
      this.propsData = {} // 避免报错
      this.propsData.outlet_info = [] // 避免报错
      this.propsData.roleOptions = this.roleOptions
      this.$refs.triggerWay.outleList()
    },

    // 子窗口关闭,刷新数据
    isrefresh(data) {
      this.fetchData()
    },

    // 编辑
    handlEdit(row) {
      this.editVisible = !this.editVisible
      this.propsData = row
      this.propsData.roleOptions = this.roleOptions
      this.$refs.triggerWay.outleList()
    },

    // 修改用户状态
    handleUserStatus(row) {
      var data = {
        'change_status': '1',
        'mobile': row.mobile,
        'id': row.id
      }
      data.status = (row.status === '1') ? '0' : '1' //  状态,2冻结,1正常
      api.editAdminApi(data).then(response => {
        if (response.data.code === 0) {
          this.$root.message('操作成功', 'success')
          row.status = data.status
        }
      })
    },

    // 删除
    handlDel(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        api.delAdminApi(params).then(response => {
          if (response.data.code === 0) {
            const index = this.listData.indexOf(row)
            this.listData.splice(index, 1)
            this.$root.message('已移除用户', 'success')
            this.total = this.total - 1
          }
        })
      }).catch(() => {
        this.$root.message('已取消删除', 'info')
      })
    },

    // 获取数据
    fetchData() {
      this.listLoading = true
      return new Promise((resolve, reject) => {
        api.adminUserApi(this.listQuery).then(response => {
          if (response.data.code === 0) {
            this.listData = response.data.data
            this.total = parseInt(response.data.total)
          }
          this.listLoading = false
        })
      })
    },

    // 获取角色列表
    roleList() {
      api.selectRoleListApi().then(response => {
        if (response.data.code === 0) {
          this.roleOptions = response.data.data
        }
      })
    }

  },

  // 监事事件
  watch: {
    outlet_id(id) {
      this.$nextTick(() => {
        setTimeout(() => {
          this.listQuery.unit_id = id
          this.fetchData()
        }, 20)
      })
    }
  }

}
</script>
